package com.rd.student.ruanjian.class3.gaochenxi.LianXi1.stringPractice;

import java.util.*;

public class Cate {
      public static void main(String[] args) {
            String str = "aaaaaaabbbbbcerrrrggggggggsssssspoqqqqq";
//            System.out.println(returnXuLie(str));让子母以字母+数字的形式连接在一起

//            HashMap map = returnHashMap(str); 让字母出现的次数以hashMap的方式显示
//            Inter(map);

            Scanner scanner=new Scanner(System.in);
            System.out.println("请输入想要生成的验证码的长度:");
            int s=scanner.nextInt();
            String s1 = returnCate(s);
            System.out.println(s1);
            //生成验证码
      }

      //想用HashMap以键值的形式返回每个字母出现的次数
      public static HashMap returnHashMap(String str) {
            HashMap hashMap = new HashMap();
            for (int i = 0; i < str.length(); i++) {
                  String str1 = "";
                  str1 = String.valueOf(str.charAt(i));
                  if (!hashMap.containsKey(str1)) {
                        hashMap.put(str1, 1);
                  } else {
                        int value = (int) hashMap.get(str1);
                        hashMap.put(str1, value + 1);
                  }
            }
            return hashMap;
      }

      public static void Inter(HashMap hashMap) {
            Set set = hashMap.keySet();
            Iterator it = set.iterator();
            while (it.hasNext()) {
                  Object key = it.next();
                  Object value = hashMap.get(key);
                  System.out.println("<" + key + "   " + value + "次" + ">");
            }
      }

      public static String returnXuLie(String str) {
            int[] array = new int[26];
            String str1 = "";
            for (int i = 0; i < str.length(); i++) {
                  array[str.charAt(i) - 'a']++;//每一个字符-'a',得到的是这个字母在这个数组中对应下标所出现的次数，str.charAt(i)表示遍历到的字符，array[]
            }
            for (int i = 0; i < array.length; i++) {
                  if (array[i] == 1) {
                        str1 = str1.concat(String.valueOf((char) ('a' + i)));
                  } else if (array[i] > 1) {
                        str1 = str1.concat((char) ('a' + i) + String.valueOf(array[i]));
                  }
            }
            return str1;
      }

      public static String returnCate(int cate) {
            StringBuilder stringBuilder = new StringBuilder();
            Random random = new Random();
            for(int i=0;i<cate;i++){
                  int key = random.nextInt(3);
                  switch (key){
                        case 0:
                              int num=random.nextInt(10);
                              stringBuilder.append(num);
                              break;
                        case 1:
                              char Upper= (char) (random.nextInt(26)+'A');
                              stringBuilder.append(Upper);
                              break;
                        case 2:
                              char Lower= (char) (random.nextInt(26)+'a');
                              stringBuilder.append(Lower);
                              break;
                  }
            }
            return stringBuilder.toString();
      }
}
